[
    {
        "id": "b56f2fb97da16d4f",
        "type": "subflow",
        "name": "Get status from MIP",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 40,
                "y": 320,
                "wires": [
                    {
                        "id": "26c74b85e997efe7"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1120,
                "y": 320,
                "wires": [
                    {
                        "id": "a73f9c9dd7594c19",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "11ed360c2a8a1f9e",
        "type": "http request",
        "z": "b56f2fb97da16d4f",
        "name": "service call to MIP",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 390,
        "y": 320,
        "wires": [
            [
                "48caeedb5ef4d9bd"
            ]
        ]
    },
    {
        "id": "26c74b85e997efe7",
        "type": "function",
        "z": "b56f2fb97da16d4f",
        "name": "build request",
        "func": "let locationId = msg.payload.data.relationships.location.data.id;\nlet ressourceId = Object.keys(msg.locationMapping).find(key => msg.locationMapping[key] === locationId);\n\nmsg.payload = { \n    \"params\": [{\n         \"acronym\": \"resource.id\",\n         \"value\": ressourceId, \n         \"operator\": \"EQUAL\" \n    }], \n    \"columns\": [\n        \"resource.id\", \n        \"resource.act.status\", \n        \"resource.act.status.text\"\n    ], \n    \"languageKey\": msg.languageKey\n}\n\nmsg.url = msg.baseUrl + \"/data/BOResource/list?X-Access-Id=\" + msg.xAccessId\n\nconst jsessionid = flow.get(\"jsessionid\")\nlet basic_encrypted = \"Basic \" + new Buffer(msg.user + ':' + msg.password).toString('base64')\n\nif (jsessionid){\n    msg.headers = {\n        \"Content-Type\": \"application/json\",\n        \"Cookie\": `JSESSIONID=${jsessionid}`\n    }\n}else{\n    msg.headers = {\n        \"Content-Type\": \"application/json\",\n        \"Authorization\": basic_encrypted,\n    };\n}\n\n\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 190,
        "y": 320,
        "wires": [
            [
                "11ed360c2a8a1f9e"
            ]
        ]
    },
    {
        "id": "a73f9c9dd7594c19",
        "type": "change",
        "z": "b56f2fb97da16d4f",
        "name": "save JSESSION",
        "rules": [
            {
                "t": "set",
                "p": "jsessionid",
                "pt": "flow",
                "to": "responseCookies.JSESSIONID.value",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 880,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "id": "48caeedb5ef4d9bd",
        "type": "switch",
        "z": "b56f2fb97da16d4f",
        "name": "Check if invalid",
        "property": "statusCode",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "401",
                "vt": "str"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 640,
        "y": 320,
        "wires": [
            [
                "8b4efebfd2c2b5ae"
            ],
            [
                "a73f9c9dd7594c19"
            ]
        ]
    },
    {
        "id": "8b4efebfd2c2b5ae",
        "type": "change",
        "z": "b56f2fb97da16d4f",
        "name": "Delete JSESSION",
        "rules": [
            {
                "t": "delete",
                "p": "jsessionid",
                "pt": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 890,
        "y": 240,
        "wires": [
            [
                "26c74b85e997efe7"
            ]
        ]
    },
    {
        "id": "2cc6b843da44183d",
        "type": "subflow",
        "name": "check alert",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 100,
                "y": 120,
                "wires": [
                    {
                        "id": "a3947a76db11217c"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 920,
                "y": 120,
                "wires": [
                    {
                        "id": "09dd93feaa112909",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "a3947a76db11217c",
        "type": "switch",
        "z": "2cc6b843da44183d",
        "name": "check if loss alert",
        "property": "payload.data.type",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "loss-alerts",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 250,
        "y": 120,
        "wires": [
            [
                "c286176d3f6f38d2"
            ]
        ]
    },
    {
        "id": "c286176d3f6f38d2",
        "type": "switch",
        "z": "2cc6b843da44183d",
        "name": "check if loss alert is answered",
        "property": "payload.data.relationships",
        "propertyType": "msg",
        "rules": [
            {
                "t": "hask",
                "v": "loss-reason",
                "vt": "str"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 490,
        "y": 120,
        "wires": [
            [],
            [
                "09dd93feaa112909"
            ]
        ]
    },
    {
        "id": "09dd93feaa112909",
        "type": "change",
        "z": "2cc6b843da44183d",
        "name": "save loss alert id",
        "rules": [
            {
                "t": "set",
                "p": "current_id",
                "pt": "msg",
                "to": "payload.data.id",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 120,
        "wires": [
            []
        ]
    },
    {
        "id": "f1c83b33ac990a56",
        "type": "tab",
        "label": "MIP respond to loss alerts",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "e2877b47cfbb6e12",
        "type": "function",
        "z": "f1c83b33ac990a56",
        "name": "construct mqtt payload",
        "func": "// dictonary mapping MIP status code to loss reason id\n\nlet loss_reason_id = msg.mapping[msg.payload[1].data[0]]\n\nmsg.payload = {\n    \"data\": {\n        \"id\": msg.current_id,\n            \"relationships\": {\n            \"loss-reason\": {\n                \"data\": {\n                    \"id\": loss_reason_id\n                }\n            }\n        }\n    }\n}\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1130,
        "y": 120,
        "wires": [
            [
                "2e95d967cf49ce27"
            ]
        ]
    },
    {
        "id": "cc85a223d382bfc9",
        "type": "mqtt in",
        "z": "f1c83b33ac990a56",
        "name": "Listen for loss alerts",
        "topic": "oee/tablet/$TABLET_ID/+/loss-alert",
        "qos": "2",
        "datatype": "json",
        "broker": "",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 170,
        "y": 120,
        "wires": [
            [
                "1f1930b70244c1db"
            ]
        ]
    },
    {
        "id": "2e95d967cf49ce27",
        "type": "mqtt out",
        "z": "f1c83b33ac990a56",
        "name": "send loss reason",
        "topic": "oee/tablet/$TABLET_ID/alert-reason",
        "qos": "2",
        "retain": "true",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "",
        "x": 1370,
        "y": 120,
        "wires": []
    },
    {
        "id": "1f1930b70244c1db",
        "type": "change",
        "z": "f1c83b33ac990a56",
        "name": "set settings",
        "rules": [
            {
                "t": "set",
                "p": "locationMapping",
                "pt": "msg",
                "to": "{\"$RESSOURCE_ID\":\"$LOCATION_ID\"}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "mapping",
                "pt": "msg",
                "to": "{\"$STATUS_NUMBER\":\"$LOSS_REASON_ID\"}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "baseUrl",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "user",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "password",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "xAccessId",
                "pt": "msg",
                "to": "",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "languageKey",
                "pt": "msg",
                "to": "en",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 470,
        "y": 120,
        "wires": [
            [
                "b3bd2453d9f7fb97"
            ]
        ]
    },
    {
        "id": "aa08bf428d805cee",
        "type": "comment",
        "z": "f1c83b33ac990a56",
        "name": "^^ configure authentication and topic",
        "info": "",
        "x": 1430,
        "y": 180,
        "wires": []
    },
    {
        "id": "add8169cbb7b7588",
        "type": "comment",
        "z": "f1c83b33ac990a56",
        "name": "^^ configure settings",
        "info": "",
        "x": 500,
        "y": 180,
        "wires": []
    },
    {
        "id": "b3bd2453d9f7fb97",
        "type": "subflow:2cc6b843da44183d",
        "z": "f1c83b33ac990a56",
        "name": "",
        "x": 660,
        "y": 120,
        "wires": [
            [
                "ba8ce949806c7074"
            ]
        ]
    },
    {
        "id": "2bf846e7eee57652",
        "type": "comment",
        "z": "f1c83b33ac990a56",
        "name": "^^ configure authentication and topic",
        "info": "",
        "x": 230,
        "y": 180,
        "wires": []
    },
    {
        "id": "ba8ce949806c7074",
        "type": "subflow:b56f2fb97da16d4f",
        "z": "f1c83b33ac990a56",
        "name": "",
        "x": 880,
        "y": 120,
        "wires": [
            [
                "e2877b47cfbb6e12"
            ]
        ]
    }
]